/*
 * File name:  PMIPActivit.java
 * Copyright:  Copyright (c) 2006-2013 hoperun Inc,  All rights reserved
 * Description:  <描述>
 * Author:  ren_qiujing
 * Last modified date:  2013-9-12
 * Version:  <版本编号>
 * Edit history:  <修改内容><修改人>
 */
package com.hopedove.base;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import androidx.lifecycle.LiveData;

import com.hopedove.utils.ToastUtil;
import com.hopedove.view.WaitDialog;

public abstract class BaseActivity extends AppCompatActivity {
    private WaitDialog mWaitDialog;

    protected final Handler uiHandler = new Handler(Looper.getMainLooper());

    public boolean isActivityDestroyed() {
        return isActivityDestroyed;
    }

    public void setActivityDestroyed(boolean isActivityDestroyed) {
        this.isActivityDestroyed = isActivityDestroyed;
    }

    protected boolean isActivityDestroyed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 设置界面无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置界面常亮
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mWaitDialog = new WaitDialog(this);
        super.onCreate(savedInstanceState);
    }


    protected void toggleWaitDialog(boolean show) {
        if (show) {
            showWait();
        } else {
            stopWait();
        }
    }

    /**
     * 显示加载圈圈...
     */
    public void showWait() {
        mWaitDialog.show();
    }

    /**
     * 停掉加载圈圈...
     */
    public void stopWait() {
        mWaitDialog.dismiss();
    }

    protected void observeLoading(LiveData<Boolean> loading) {
        loading.observe(this, show -> {
            if (show) {
                showWait();
            } else {
                stopWait();
            }
        });
    }

    protected void observeNavigation(LiveData<Class<?>> event) {
        event.observe(this, clazz -> {
            startActivity(new Intent(this, clazz));
        });
    }

    protected void observeToastText(LiveData<String> error) {
        error.observe(this, text -> {
            ToastUtil.longToast(this, text);
        });
    }


    protected final Drawable getDrawableCompat(@DrawableRes int id) {
        return ResourcesCompat.getDrawable(getResources(), id, getTheme());
    }

    protected final int getColorCompat(@ColorRes int id) {
        return ResourcesCompat.getColor(getResources(), id, getTheme());
    }

    @Override
    protected void onDestroy() {
        isActivityDestroyed = true;
        super.onDestroy();
    }

}
